package me.learningandroid.ch2_3.permissiondemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.CombinedVibration;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.os.VibratorManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    private static int REQUEST_CALL_PERMISSION=1;
    private Vibrator vibrator;

    @RequiresApi(api = Build.VERSION_CODES.S)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vibrator = getSystemService(Vibrator.class);


        Button btn1 = (Button) findViewById(R.id.button);
        Button btn2 = (Button) findViewById(R.id.button2);
        Button btn3 = (Button) findViewById(R.id.button3);
        EditText textView = (EditText) findViewById(R.id.editText);

        btn1.setOnClickListener((view)-> {
            String phoneNum = textView.getText().toString();
            Intent intent=new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(String.format("tel:%s", phoneNum)));
            startActivity(intent);
        });

        btn3.setOnClickListener((view)-> {
            vibrator.vibrate(VibrationEffect.createOneShot(100,VibrationEffect.DEFAULT_AMPLITUDE));
            Toast.makeText(this, "震动",1000).show();
        });

        btn2.setOnClickListener((view)-> {
            // ContextCompat.checkSelfPermission 判断应用是否已经获得权限
            if (ContextCompat.checkSelfPermission(
                    this, Manifest.permission.CALL_PHONE) ==
                    PackageManager.PERMISSION_GRANTED) {
                String phoneNum = textView.getText().toString();
                doCallAction(phoneNum);
            } else if (shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) {
                //  shouldShowRequestPermissionRationale当用户拒绝过该权限并且没有选择“不再提醒”，给开发人员提供的详细提示的机会
                // 参考 https://blog.csdn.net/yeluofengchui/article/details/91126163

                // 这里应该提供一个对话框，提供更详细的权限信息，提供确认和取消按钮，确认按钮触发系统的授权行为。
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("再用户拒绝权限后，再次需要用到功能时，开发人员在这里提供更详细的权限信息。是否继续？")
                        .setPositiveButton("好的", (dialog, which)->{
                            // requestPermission 申请权限，申请后系统会异步调用onRequestPermissionsResult
                            // 参数permissions为需要申请的权限列表
                            // 参数requestId是请求id，开发人员通过请求id将requestPermission和onRequestPermissionsResult相关联。
                            requestPermissions(new String[]{Manifest.permission.CALL_PHONE},REQUEST_CALL_PERMISSION);
                        })
                        .setNegativeButton("取消", (dialog, which)->{}).show();
            } else {
                // 系统的授权行为

                requestPermissions(new String[]{Manifest.permission.CALL_PHONE},REQUEST_CALL_PERMISSION);
            }
        });
    }

    private void doCallAction(String phoneNum) {
        Intent intent=new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse(String.format("tel:%s", phoneNum)));
        startActivity(intent);
    }

    // onRequestPermissionsResult在系统对应用授权后调用
    // 参数requestId是请求id，开发人员通过请求id将requestPermission和onRequestPermissionsResult相关联。
    // 参数requested为申请权限列表
    // 参数granted为申请结果,1为成功，0为拒绝，一一对应requested
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] requested, int[] granted) {
        if (requestCode==REQUEST_CALL_PERMISSION) {
            if (granted.length==1 && granted[0]==1) {
                EditText textView = (EditText) findViewById(R.id.editText);
                String phoneNum = textView.getText().toString();
                doCallAction(phoneNum);
            }
        }
    }
}